﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using ZP.DAL;
using ZP.IDAL;
using AutoMapper;
using ZP.Model;
using ZP.DTO.Common;
using ZP.DTO.fz_sys_user;
using ZP.DTO.fz_sys_menu;
using ZP.DTO.fz_sys_role;
using ZP.DTO.fz_base_employee;

namespace ZP.DALContainer
{
    public class Container
    {
        private static object _objLock = new object();
        private static bool _initFlag = false;

        /// <summary>
        /// IOC 容器
        /// </summary>
        //public static IContainer container = null;

        /// <summary>
        /// 获取 IDal 的实例化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        //public static T Resolve<T>()
        //{
        //    lock (_objLock)
        //    {
        //        try
        //        {
        //            if (container == null)
        //            {
        //                Initialise();
        //            }
        //        }
        //        catch (System.Exception ex)
        //        {
        //            throw new System.Exception("IOC实例化出错!" + ex.Message);
        //        }
        //        return container.Resolve<T>();
        //    }
        //}

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialise()
        {
            /*var builder = new ContainerBuilder();
            //格式：builder.RegisterType<xxxx>().As<Ixxxx>().InstancePerLifetimeScope();
            //builder.RegisterType<fz_sys_userDAL>().As<Ifz_sys_userDAL>().InstancePerLifetimeScope();
            builder.RegisterType<fz_sys_userDAL>().As<Ifz_sys_userDAL>().InstancePerLifetimeScope();
            builder.RegisterType<fz_sys_menuDAL>().As<Ifz_sys_menuDAL>().InstancePerLifetimeScope();
            builder.RegisterType<fz_sys_roleDAL>().As<Ifz_sys_roleDAL>().InstancePerLifetimeScope();
            builder.RegisterType<fz_base_employeeDAL>().As<Ifz_base_employeeDAL>().InstancePerLifetimeScope();

            container = builder.Build();*/

            #region 实体和Model映射
            if (!_initFlag)
            {
                Mapper.Initialize(cfg =>
                {
                    cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;

                    //fz_sys_user
                    cfg.CreateMap<fz_sys_user, fz_sys_userResultDTO>().ReverseMap();
                    cfg.CreateMap<fz_sys_user, fz_sys_userOperateDTO>().ReverseMap();
                    cfg.CreateMap<fz_sys_user, UserLoginDTO>().ReverseMap();

                    //fz_sys_menu
                    cfg.CreateMap<fz_sys_menu, fz_sys_menuResultDTO>().ReverseMap();
                    cfg.CreateMap<fz_sys_menu, fz_sys_menuOperateDTO>().ReverseMap();
                    cfg.CreateMap<fz_sys_menu, UserLoginDTO.CurrentUserAuthority>().ReverseMap();

                    //fz_sys_role
                    cfg.CreateMap<fz_sys_role, fz_sys_roleResultDTO>().ReverseMap();
                    cfg.CreateMap<fz_sys_role, fz_sys_roleOperateDTO>().ReverseMap();

                    //fz_base_employee
                    cfg.CreateMap<fz_base_employee, fz_base_employeeResultDTO>().ReverseMap();
                    cfg.CreateMap<fz_base_employee, fz_base_employeeOperateDTO>().ReverseMap();

                });
                _initFlag = true;
            }
            #endregion
        }
    }
}
